package com.google.gwt.mobile.widgets.ui;

import com.google.gwt.dom.client.Element;
import com.google.gwt.mobile.widgets.transition.HasTransition;
import com.google.gwt.mobile.widgets.transition.Transition;
import com.google.gwt.mobile.widgets.utils.Utils;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.RootLayoutPanel;

/**
 * The Class TransitionWidget.
 */
public abstract class TransitionWidget extends Composite implements HasTransition {

  private static Transition _defaultTransition = Transition.SLIDE;
  private boolean _isInitialLoad = true;
  private Transition _transition;

  @Override
  public void onLoad() {
    super.onLoad();

    if (_isInitialLoad) {
      _isInitialLoad = false;
      onInitialLoad();
    }
  }

  /*
   * (non-Javadoc)
   * 
   * @see com.google.gwt.mobile.widgets.transition.HasTransition#onInitialLoad
   * ()
   */
  @Override
  public void onInitialLoad() {
  }

  /*
   * (non-Javadoc)
   * 
   * @see com.google.gwt.mobile.widgets.transition.HasTransition#onTransitionEnd
   * ()
   */
  @Override
  public void onTransitionEnd() {
  }

  public void goTo(final TransitionWidget to) {
    goTo(to, _defaultTransition);
  }

  public void goTo(final TransitionWidget to, final Transition transition) {
    Element focus = Utils.getActiveElement();
    focus.blur();
    final TransitionWidget from = this;
    to.setTransition(transition);

    if (transition != null) {
      transition.start(from, to, RootLayoutPanel.get(), false);
    } else {
      Transition.start(from, to, RootLayoutPanel.get());
    }
  }

  public Transition getTransition() {
    return _transition;
  }

  public void setTransition(final Transition transition) {
    _transition = transition;
  }
}
